package com.treasure.hunt.task

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.mode.ViewModeFragment
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.treasure.hunt.R
import com.treasure.hunt.entity.LuckyRanking
import com.treasure.hunt.mode.GridViewMode
import kotlinx.android.synthetic.main.fragment_grid_ranking.*

class GridRankingFragment : ViewModeFragment<GridViewMode>() {

    companion object {
        fun newInstance(): GridRankingFragment {
            return GridRankingFragment()
        }
    }

    private val rankingAdapter =
        object : BaseQuickAdapter<LuckyRanking, BaseViewHolder>(R.layout.item_grid_ranking) {
            override fun convert(holder: BaseViewHolder, item: LuckyRanking) {
                holder.setText(R.id.tvRankingNickname, item.nickname)
                holder.setText(R.id.tvRankingClearance, item.num.toString())
                holder.setText(R.id.tvRankingRank, (holder.layoutPosition + 1).toString())
                val tvRankingIcon: ImageView = holder.getView(R.id.tvRankingIcon)
                val position: Int = holder.layoutPosition
                if (position == 0) {
                    tvRankingIcon.visibility = View.VISIBLE
                    tvRankingIcon.setImageResource(R.mipmap.ic_23312)
                } else if (position == 1) {
                    tvRankingIcon.visibility = View.VISIBLE
                    tvRankingIcon.setImageResource(R.mipmap.ic_23313)
                } else if (position == 2) {
                    tvRankingIcon.visibility = View.VISIBLE
                    tvRankingIcon.setImageResource(R.mipmap.ic_23314)
                } else {
                    tvRankingIcon.visibility = View.INVISIBLE
                }
            }
        }

    override fun viewModel(): GridViewMode {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.fragment_grid_ranking

    override fun initView() {
        rvGridRankingBody.adapter = rankingAdapter
    }

    override fun onResume() {
        super.onResume()
        viewModel.luckyTurntableRanking()
    }

    override fun initViewMode() {
        viewModel.luckyRankingLiveData.observe(viewLifecycleOwner, Observer { response ->
            if (response.status == Status.FAILURE) {
                rankingAdapter.setEmptyView(createErrorView(response.throwable?.msg ?: "未知错误"))
            }
            if (response.status != Status.SUCCESS) {
                return@Observer
            }
            response.httpData?.data?.let {
                if (it.size == 0) {
                    rankingAdapter.setEmptyView(createErrorView("暂无数据~"))
                    return@Observer
                }
                LogUtils.e(GsonUtils.toJson(it))
                rankingAdapter.setNewInstance(it)
            }
        })
    }


//    private fun createErrorView(hint: String): View =
//        TextView(requireContext()).apply {
//            text = hint
//            gravity = Gravity.CENTER
//            textSize = 13f
//            setTypeface(null, Typeface.BOLD)
//            setTextColor(ColorUtils.getColor(R.color.FF_BEC0E1))
//            setOnClickListener {
//
//            }
//        }

    private fun createErrorView(message: String? = null): View {
        return layoutInflater.inflate(R.layout.view_empty_error, null).apply {
            if (message != null)
                findViewById<TextView>(R.id.tvEmptyMessage).text = message
        }
    }
}